package com.dhwl.hsh.merchant.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;

import com.dhwl.hsh.lib.constant.ServerConstant;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.MvpView;
import com.dhwl.hsh.lib.utils.StringUtils;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.merchant.presenter.ForgotPwdPresenter;
import com.dhwl.hsh.merchant.view.DeleteableEditText;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 忘记密码页面
 *
 * @author Derek.Kidd
 * @time 2016/4/15 11:30
 */
public class ForgotPwdActivity extends BaseActivity implements MvpView {
    @Bind(R.id.edit_forgot_pwd_phone)
    DeleteableEditText editPhone;

    @Bind(R.id.edit_verification_code)
    DeleteableEditText editVcode;

    @Bind(R.id.btn_get_verification_code)
    Button btnGetVCode;

    @Bind(R.id.btn_forgot_pwd_next)
    Button btnNext;

    private String vCode;

    private String phoneNum;

    private static final int REQUESTCODE = 6;

    ForgotPwdPresenter forgotPwdPresenter;

    private CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            btnGetVCode.setEnabled(false);
            //            btnGetVCode.setBackgroundResource(Color.TRANSPARENT);
            btnGetVCode.setBackgroundResource(R.drawable.selector_bg_stroke_orange);
            btnGetVCode.setTextColor(Color.WHITE);
            btnGetVCode.setText((millisUntilFinished / 1000) + getText(R.string.resend_vcode).toString());
        }

        @Override
        public void onFinish() {
            btnGetVCode.setEnabled(true);
            btnGetVCode.setBackgroundResource(R.drawable.selector_bg_stroke_orange);
            btnGetVCode.setTextColor(Color.WHITE);
            //btnGetVCode.setText(R.string.get_vcode);
            btnGetVCode.setText(R.string.reget);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forgot_pwd;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setTitleContent(R.string.title_forgot_pwd);
        editPhone.setMyTextWatcher(new DeleteableEditText.MyTextWatcher() {
            @Override
            public void afterTextChanged() {
                showSubmitBtn();
            }
        });
    }

    @Override
    protected void initData() {

        forgotPwdPresenter = new ForgotPwdPresenter();
        forgotPwdPresenter.attachView(this);
    }


    /**
     * 控制提交按钮是否可以使用
     *
     * @see [类、类#方法、类#成员]
     */
    private void showSubmitBtn() {
        phoneNum = editPhone.getText();
        vCode = editVcode.getText();
        //手机号不为空并且符合规则时才可点击获取验证码按钮

        if (!StringUtils.isEmpty(phoneNum) && StringUtils.isPhone(phoneNum)) {
            btnGetVCode.setEnabled(true);
            btnGetVCode.setBackgroundResource(R.drawable.selector_bg_stroke_orange);
        } else {
            btnGetVCode.setEnabled(false);
            btnGetVCode.setBackgroundResource(R.drawable.shape_bg_stroke_gray);
        }

        if (!StringUtils.isEmpty(phoneNum) && !StringUtils.isEmpty(vCode) && StringUtils.isPhone(phoneNum)
                && vCode.length() >= ServerConstant.VCODE_LENGTH) {
            btnNext.setEnabled(true);
            btnNext.setBackgroundResource(R.drawable.selector_comm_btn);

        } else {
            btnNext.setBackgroundResource(R.drawable.comm_bg_btn_grey);
            btnNext.setEnabled(false);
        }

    }

    @OnClick(R.id.btn_get_verification_code)
    public void onGetVcodeClick(View v) {
        // 获取字段信息并验证
        phoneNum = editPhone.getText();
        // 账号为空，返回
        if (StringUtils.isEmpty(phoneNum)) {
            showToast(R.string.user_login_validate_phone_is_null);
            return;
        }
        // 账号手机号规则验证
        if (!StringUtils.isPhone(phoneNum)) {
            showToast(R.string.input_phone_is_error);
            return;
        }
        showWaitingDialog();
        //替换目前验证方式为我们自己服务端统一验证:获取验证码
        forgotPwdPresenter.getVCode(phoneNum, 1);
    }

    @OnClick(R.id.btn_forgot_pwd_next)
    public void onNextActionClick(View v) {
        // 获取字段信息并验证
        phoneNum = editPhone.getText();
        // 账号为空，返回
        if (StringUtils.isEmpty(phoneNum)) {
            showToast(R.string.user_login_validate_phone_is_null);
            return;
        }
        // 账号手机号规则验证
        if (!StringUtils.isPhone(phoneNum)) {
            showToast(R.string.input_phone_is_error);

            return;
        }
        // 获取字段信息并验证
        vCode = editVcode.getText();
        // 验证码为空，返回
        if (StringUtils.isEmpty(vCode)) {
            showToast(R.string.toast_vcode_is_null);
            return;
        }
        // 账号手机号规则验证
        if (vCode.length() < ServerConstant.VCODE_LENGTH) {
            showToast(R.string.toast_vcode_failure);
            return;
        }


        //替换目前验证方式为我们自己服务端统一验证:验证验证码
        checkVCHttpRequest();
    }

    @Override
    protected void onDestroy() {
        countDownTimer.cancel();
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //重置密码成功后
        if (REQUESTCODE == requestCode && RESULT_OK == resultCode) {
            //返回登录页面
            startActivity(new Intent(this, LoginActivity.class));
            Intent intent = new Intent();
            intent.putExtra(Constant.UserInfo.KEY_PHONE_NUM, phoneNum);
            finish();
        }
    }


    /**
     * 验证验证码
     *
     * @see [类、类#方法、类#成员]
     */
    private void checkVCHttpRequest() {
        Intent intent = new Intent(ForgotPwdActivity.this, ResetLoginPwdActivity.class);
        intent.putExtra(Constant.UserInfo.KEY_PHONE_NUM, phoneNum);
        intent.putExtra(Constant.UserInfo.KEY_V_CODE, vCode);
        startActivityForResult(intent, REQUESTCODE);
        startActivity(intent);
    }


    @Override
    public void onFailure(HshException e) {

    }

    @Override
    public void onSuccess(Object object) {

    }
}